﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dysmsapi.Model.V20170525;

namespace DTcms.Web.m
{
    /// <summary>
    /// call_log 的摘要说明
    /// </summary>
    public class call_log : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            int _send_user_id = DTRequest.GetFormInt("send_user_id", 0);
            string _user_name = DTRequest.GetFormString("txtUserName");
            string _user_pwd= DTRequest.GetFormString("user_pwd");
            string _channel_name = DTRequest.GetFormString("channel_name");
            int _article_id = DTRequest.GetFormInt("article_id", 0);
            Model.users userModel = new BLL.users().GetModel(_user_name, _user_pwd, 0, 1, true);
            if (userModel == null)
            {
                Utils.WriteTextLog("call_log", "username:" + _send_user_id + ",pwd:" + _user_pwd + "您的登录信息有误，请退出系统后重新登录！", DateTime.Now);
                context.Response.Write("{\"status\":0, \"msg\":\"错误提示：用户名或密码错误！\"}");
                return;
            }
            

            if (string.IsNullOrWhiteSpace(_user_pwd)) {
                context.Response.Write("{\"status\": 0}");
                return;
            }
            if (string.IsNullOrEmpty(_channel_name))
            {
                context.Response.Write("{\"status\": 0}");
                return;
            }
            if (_article_id == 0)
            {
                context.Response.Write("{\"status\": 0}");
                return;
            }

            //int _to_user_id = DTRequest.GetFormInt("to_user_id", 0);
            //string _to_user_name = DTRequest.GetFormString("to_user_name");
            //string _mobile = DTRequest.GetFormString("mobile");

            int _to_userid = 0;
            string _to_username = string.Empty;
            string _mobile = string.Empty;
            string _send_mobile = new BLL.users().GetMobile(_send_user_id);
            //if (!new BLL.users().Exists(_send_user_id)) {
            //    context.Response.Write("{\"status\": 0}");
            //}
            switch (_channel_name)
            {
                case "qiugou":
                    BLL.qiugou bllQiugou = new BLL.qiugou();
                    Model.qiugou modelQiugou = bllQiugou.GetModel(_article_id);
                    if (modelQiugou != null)
                    {
                        _to_userid = modelQiugou.user_id;
                        if (modelQiugou.user_id > 0)
                        {
                            _to_username = new BLL.users().GetUserName(modelQiugou.user_id);
                        }
                        else
                        {
                            _to_username = modelQiugou.lianxiren;
                        }
                        _mobile = modelQiugou.mobile;
                    }
                    break;
                case "gongying":
                    BLL.gongying bllGongying = new BLL.gongying();
                    Model.gongying modelGongying = bllGongying.GetModel(_article_id);
                    if (modelGongying != null)
                    {
                        _to_userid = modelGongying.user_id;
                        if (modelGongying.user_id > 0)
                        {
                            _to_username = new BLL.users().GetUserName(modelGongying.user_id);
                        }
                        else
                        {
                            _to_username = modelGongying.lianxiren;
                        }
                        _mobile = modelGongying.mobile;
                    }
                    break;
                case "job":
                    BLL.jobs bllJob = new BLL.jobs();
                    Model.jobs modelJob = bllJob.GetModel(_article_id);
                    if (modelJob != null)
                    {
                        _to_userid = modelJob.user_id;
                        if (modelJob.user_id > 0)
                        {
                            _to_username = new BLL.users().GetUserName(modelJob.user_id);
                        }
                        else
                        {
                            _to_username = modelJob.lianxiren;
                        }
                        _mobile = modelJob.mobile;
                    }
                    break;
                case "wuliu":
                    BLL.wuliu bllWuliu = new BLL.wuliu();
                    Model.wuliu modelWuliu = bllWuliu.GetModel(_article_id);
                    if (modelWuliu != null)
                    {
                        _to_userid = modelWuliu.user_id;
                        if (modelWuliu.user_id > 0)
                        {
                            _to_username = new BLL.users().GetUserName(modelWuliu.user_id);
                        }
                        else
                        {
                            _to_username = modelWuliu.lianxiren;
                        }
                        _mobile = modelWuliu.mobile;
                    }
                    break;
                case "company":
                    BLL.company bllCompany = new BLL.company();
                    Model.company modelCompany = bllCompany.GetModel(_article_id);
                    if (modelCompany != null)
                    {
                        _to_userid = modelCompany.user_id;
                        if (modelCompany.user_id > 0)
                        {
                            _to_username = new BLL.users().GetUserName(modelCompany.user_id);
                        }
                        else
                        {
                            _to_username = modelCompany.lianxiren;
                        }
                        _mobile = modelCompany.mobile;
                    }
                    break;
                case "xinxi":
                    BLL.xinxi bllXinxi = new BLL.xinxi();
                    Model.xinxi modelXinxi = bllXinxi.GetModel(_article_id);
                    if (modelXinxi != null)
                    {
                        _to_userid = modelXinxi.user_id;
                        if (modelXinxi.user_id > 0)
                        {
                            _to_username = new BLL.users().GetUserName(modelXinxi.user_id);
                        }
                        else
                        {
                            _to_username = modelXinxi.lianxiren;
                        }
                        _mobile = modelXinxi.mobile;
                    }
                    break;
            }

            BLL.call_log bll = new BLL.call_log();
            DateTime d = DateTime.Now;
            string _date = d.Year + "-" + (d.Month < 10 ? "0" + d.Month : d.Month + "") + "-" + (d.Day < 10 ? "0" + d.Day : d.Day + "");
            if (!bll.Exists(_send_user_id, _to_userid, _mobile, _date))
            {

                Model.call_log model = new Model.call_log();

                model.send_user_id = _send_user_id;
                model.to_user_id = _to_userid;
                model.to_user_name = _to_username;
                model.mobile = _mobile;
                model.add_time = DateTime.Now;
                model.channel_name = _channel_name;
                model.article_id = _article_id;
                model.send_mobile = _send_mobile;

                int result = bll.Add(model);

                //开始发送短信
                if (result > 0)
                {
                    String product = "Dysmsapi";//短信API产品名称
                    String domain = "dysmsapi.aliyuncs.com";//短信API产品域名
                    String accessKeyId = "LTAICl1VBGDdDWNy";//你的accessKeyId
                    String accessKeySecret = "txDFRBMf0PmE1pazwgE3BL3N8xa0mi";//你的accessKeySecret

                    string strcode = Utils.Number(5); //随机验证码

                    IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);
                    //IAcsClient client = new DefaultAcsClient(profile);
                    // SingleSendSmsRequest request = new SingleSendSmsRequest();

                    DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
                    IAcsClient acsClient = new DefaultAcsClient(profile);
                    SendSmsRequest request = new SendSmsRequest();
                    try
                    {
                        //必填:待发送手机号。支持以逗号分隔的形式进行批量调用，批量上限为20个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
                        request.PhoneNumbers = _mobile;
                        //必填:短信签名-可在短信控制台中找到
                        request.SignName = "中国石材圈";
                        //必填:短信模板-可在短信控制台中找到
                        request.TemplateCode = "SMS_107875019";
                        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
                        request.TemplateParam = "{\"code\":\"" + _send_mobile + "\"}";
                        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
                        request.OutId = "21212121211";
                        //请求失败这里会抛ClientException异常
                        SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);
                        string result_message = sendSmsResponse.Message;
                    }
                    catch (ServerException ee)
                    {
                        //Response.Write(ee.ErrorCode+ee.ErrorMessage);
                    }
                }
            }


            context.Response.Write("{\"status\": 1}");
            return;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}